$(function() {
    initArtCateList()
        // 获取文章分类列表
    function initArtCateList() {
        $.ajax({
            type: "get",
            url: "/my/article/cates",
            success: function(res) {
                let htmlStr = template("tpl-table", res)
                $("tbody").html(htmlStr)
            }
        });
    }
    let openLog = null
    $("#btnAddList").on("click", function() {
        openLog = layer.open({
            type: 1,
            area: ['500px', '250px'],
            title: "添加文章分类",
            content: $("#dialog-add").html()
        });
    })
    $("body").on("submit", "#form-add", function(e) {
        e.preventDefault();
        $.ajax({
            type: "post",
            url: "/my/article/addcates",
            data: $(this).serialize(),
            success: function(res) {
                // console.log(res);
                if (res.status !== 0) {
                    return layer.msg("添加失败！")
                }
                layer.msg("添加成功！")
                initArtCateList()
                layer.close(openLog);
            }
        });
    })
    let edit = null
    let form = layui.form
    $("tbody").on("click", "#btnEdit", function() {
        let id = $(this).attr("data-id");
        edit = layer.open({
            type: 1,
            area: ['500px', '250px'],
            title: "修改文章分类",
            content: $("#dialog-edit").html()
        });
        // console.log(id);
        // let a = $(this).parent().siblings()
        // $("#cname").val(a[0].innerHTML)
        // $("#bname").val(a[1].innerHTML)
        $.ajax({
            type: "get",
            url: "/my/article/cates/" + id,
            success: function(res) {
                form.val('form-edit', res.data)
            }
        });
    })
    $("body").on("submit", "#form-edit", function(e) {
        // let id = $("#btnEdit").attr("data-id");
        // let a = $("#btnEdit").parent().siblings()
        // a[0].innerHTML = $("#cname").val()
        // a[1].innerHTML = $("#bname").val()
        e.preventDefault();
        // console.log(a[0].innerHTML);
        $.ajax({
            method: "post",
            url: "/my/article/updatecate",
            data: $(this).serialize(),
            success: function(res) {
                console.log(res);
                if (res.status) {
                    return layer.msg("更新失败！")
                }
                layer.msg("更新成功！")
                layer.close(edit)
                initArtCateList()
            }
        });
    })
    $("tbody").on("click", "#btnDel", function() {
        let id = $(this).attr("data-id");
        layer.confirm('确定删除？', { icon: 3, title: '提示' }, function(index) {
            $.ajax({
                type: "get",
                url: "/my/article/deletecate/" + id,
                success: function(res) {
                    if (res.status) {
                        return layer.msg("删除失败！")
                    }
                    layer.close(index)
                    initArtCateList()
                    layer.msg("删除成功！")
                }
            });
        });
    })
})